//
//  PswInputView.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/8.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "PswInputView.h"
#import <Masonry/Masonry.h>

@implementation PswInputView

+ (instancetype)initWithFrame:(CGRect)frame money:(NSString *)money psdBlock:(PsdBlock)psdStr forgetPsd:(ForgetPsd)porget
{
    
    return [[PswInputView alloc] initWithFrame:frame block:psdStr withMoney:money forgetPsd:porget];
}
- (instancetype)initWithFrame:(CGRect)frame block:(PsdBlock)block withMoney:(NSString *)money forgetPsd:(ForgetPsd)porget
{
    if (self = [super initWithFrame:frame]) {
        
        
        _psdBlock = block;
        _money = money;
        _forgetPsd = porget;
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        [window addSubview:self];
        
        self.backgroundColor = [UIColor colorWithWhite:.2 alpha:.5];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selfHide)];
        [self addGestureRecognizer:tap];
        [self addSubview:self.bgView];
        [self setUI];
        [self setMima];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(errorMessage:) name:@"payMessage" object:nil];
    }
    return self;
}
- (void)errorMessage:(NSNotification*)noti
{
    //    NSDictionary *info = noti.userInfo;
    
}
- (void)setMima
{
    
    NSLog(@"%s",__func__);
    [self addSubview:self.psdField];
    
    
    
    [_psdField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        
        NSLog(@"%s===%@",__func__,x);
        
        switch (x.length) {
            case 0:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = YES;
                img2.hidden = YES;
                img3.hidden = YES;
                img4.hidden = YES;
                img5.hidden = YES;
                img6.hidden = YES;
                
            }
                break;
            case 1:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = NO;
                img2.hidden = YES;
                img3.hidden = YES;
                img4.hidden = YES;
                img5.hidden = YES;
                img6.hidden = YES;
            }
                break;
            case 2:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = NO;
                img2.hidden = NO;
                img3.hidden = YES;
                img4.hidden = YES;
                img5.hidden = YES;
                img6.hidden = YES;
            }
                break;
            case 3:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = NO;
                img2.hidden = NO;
                img3.hidden = NO;
                img4.hidden = YES;
                img5.hidden = YES;
                img6.hidden = YES;
            }
                break;
            case 4:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = NO;
                img2.hidden = NO;
                img3.hidden = NO;
                img4.hidden = NO;
                img5.hidden = YES;
                img6.hidden = YES;
            }
                break;
            case 5:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = NO;
                img2.hidden = NO;
                img3.hidden = NO;
                img4.hidden = NO;
                img5.hidden = NO;
                img6.hidden = YES;
            }
                break;
            case 6:
            {
                UIImageView *img1 = [_psdView viewWithTag:10001];
                UIImageView *img2 = [_psdView viewWithTag:10002];
                UIImageView *img3 = [_psdView viewWithTag:10003];
                UIImageView *img4 = [_psdView viewWithTag:10004];
                UIImageView *img5 = [_psdView viewWithTag:10005];
                UIImageView *img6 = [_psdView viewWithTag:10006];
                
                img1.hidden = NO;
                img2.hidden = NO;
                img3.hidden = NO;
                img4.hidden = NO;
                img5.hidden = NO;
                img6.hidden = NO;
                
                _psdBlock(x,self);
                [self showHUDLoading];
                _psdField.text = @"";
                [self endEditing:YES];
                
                
                
            }
                break;
            default:
                break;
        }
        
    }];
}
- (void)setUI
{
    
    // X号按钮
    
    [_bgView addSubview:self.deleteBtn];
    
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-15);
        
    }];
    
    [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self removeFromSuperview];
    }];
    
    // 标题
    
    [_bgView addSubview:self.titleLab];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(_deleteBtn);
    }];
    
    // 分割线
    
    [_bgView addSubview:self.lineView];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(0);
        make.top.mas_equalTo(40);
        make.height.mas_equalTo(1);
    }];
    
    // 钱
    
//    [_bgView addSubview:self.moneyLab];
//
//    [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(41);
//        make.height.mas_equalTo(56);
//        make.left.and.right.mas_equalTo(0);
//
//    }];
    
    // 密码背景图
    
    [_bgView addSubview:self.psdView];
    
    [_psdView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_lineView.mas_bottom).with.offset(25);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(42);
    }];
    
    // 密码分割线
    
    NSMutableArray *fengGeViews = [NSMutableArray array];
    for (NSInteger i = 0; i < 5; i++) {
        UIView *fengGeView = [[UIView alloc] init];
        fengGeView.backgroundColor = Normal_GrayColor;
        [_psdView addSubview:fengGeView];
        [fengGeViews addObject:fengGeView];
    }
    CGFloat padding = (300-30)/6.0;
    NSLog(@"%f",self.width);
    [fengGeViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:.5 leadSpacing:padding tailSpacing:padding];
    [fengGeViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_psdView).offset(0);
        make.height.equalTo(_psdView.mas_height);
    }];
    // 密码img
    NSMutableArray *imgArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 6; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"密码icon"]];
        imgView.tag = 10001+i;
        [_psdView addSubview:imgView];
        [imgArr addObject:imgView];
    }
    [imgArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:6 leadSpacing:(padding/2.0-3) tailSpacing:(padding/2.0-3)];
    
    [imgArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_psdView);
        make.height.mas_equalTo(6);
    }];
    
    // 忘记密码
    [_bgView addSubview:self.forgetBtn];
    [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_psdView).mas_equalTo(50);
        make.right.mas_equalTo(-20);
    }];
    
    // 错误提示
    [_bgView addSubview:self.errorMessageLab];
    [_errorMessageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_psdView).mas_equalTo(30);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(20);
    }];
    WLGetWeakSelf(weakSelf, self);
    [[_forgetBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        // 忘记密码
        [weakSelf removeFromSuperview];
        _forgetPsd();
    }];
}

/**
 懒加载
 */
- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.bounds = CGRectMake(0, 0, 300, 170);
        _bgView.center = self.center;
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.tag = 100;
        _bgView.layer.cornerRadius = 5;
        _bgView.clipsToBounds = YES;
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}
- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setImage:[UIImage imageNamed:@"calcu_close"] forState:UIControlStateNormal];
        
    }
    return _deleteBtn;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = Normal_333Color;
        _titleLab.font = Normal_16Font;
        _titleLab.text = @"验证交易密码";
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}
- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#cccccc"];
    }
    return _lineView;
}
- (UILabel *)moneyLab
{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.textColor = Normal_333Color;
        _moneyLab.font = Normal_16Font;
        NSMutableAttributedString *attributStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",_money]];
        [attributStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],
                                     NSForegroundColorAttributeName:Normal_RedColor} range:NSMakeRange(1, attributStr.length-1)];
        _moneyLab.attributedText = attributStr;
        _moneyLab.textAlignment = NSTextAlignmentCenter;
    }
    return _moneyLab;
}
- (UIView *)psdView
{
    if (!_psdView) {
        _psdView = [[UIView alloc] init];
        _psdView.layer.borderColor = [UIColor colorWithHexString:@"#e5e5e5"].CGColor;
        _psdView.layer.borderWidth = 1.0;
        _psdView.layer.cornerRadius = 2.0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        _psdView.clipsToBounds = YES;
        [_psdView addGestureRecognizer:tap];
    }
    return _psdView;
}
- (void)tapClick
{
    [self.psdField becomeFirstResponder];
}
- (UIImageView *)psdImgView
{
    if (!_psdImgView) {
        _psdImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"密码icon"]];
    }
    
    return _psdImgView;
}
- (UIButton *)forgetBtn
{
    if (!_forgetBtn) {
        _forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetBtn setTitleColor:[UIColor colorWithHexString:@"0f90e2"] forState:UIControlStateNormal];
        _forgetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return _forgetBtn;
}
- (UITextField *)psdField
{
    if (!_psdField) {
        _psdField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        _psdField.keyboardType = UIKeyboardTypePhonePad;
        _psdField.returnKeyType = UIReturnKeySend;
        [_psdField becomeFirstResponder];
    }
    return _psdField;
}
- (UILabel *)errorMessageLab
{
    if (!_errorMessageLab) {
        _errorMessageLab = [[UILabel alloc] init];
        _errorMessageLab.textColor = Normal_RedColor;
        _errorMessageLab.font = [UIFont systemFontOfSize:10];
        _errorMessageLab.text = @"";
        _errorMessageLab.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        _errorMessageLab.textAlignment = NSTextAlignmentCenter;
    }
    return _errorMessageLab;
}
- (void)keyboardWillShow:(NSNotification *)notification
{
    
    
    [UIView animateWithDuration:.8 animations:^{
        self.frame = CGRectMake(0, -100, f_Device_w, f_Device_h);
    }];
    
}
- (void)keyboardWillHide:(NSNotification *)notification

{
    [UIView animateWithDuration:.8 animations:^{
        self.frame = CGRectMake(0, 0, f_Device_w, f_Device_h);
    }];
}
- (void)selfHide
{
    [self removeFromSuperview];
}
@end

